<template>
  <div class="person">
    <h2>我是Person组件，下面有{{imgList.length}}张图片</h2>
    <input type="text" placeholder="名字" v-model="name">
    <input type="text" placeholder="年龄" v-model.number="age">
    <button @click="addPerson">添加</button>
    <ul>
      <li v-for="p in personList" :key="p.id">{{p.name}}--{{p.age}}</li>
    </ul>
  </div>
</template>

<script>
  import { mapState } from 'vuex'
  import {v4 as uuid} from 'uuid'

  export default {
    name:'Person',
    data() {
      return {
        name:'',
        age:''
      }
    },
    computed:{
      ...mapState(['personList','imgList'])
    },
    methods: {
      addPerson(){
        // 1.把用户输入的信息，包装成一个人的对象
        let obj = {id:uuid(),name:this.name,age:this.age}
        // 2.想办法联系vuex，走流程添加一个人
        this.$store.dispatch('addPerson',obj)
      }
    },
  }
</script>

<style scoped>
  .person {
    background-color: orange;
    padding: 10px;
    border-radius: 10px;
    box-shadow: 0 0 10px black;
  }
  input{
    margin-right: 10px;
    width: 100px;
  }
</style>